package com.ydl.learning.flink.demo.transformations

import com.ydl.learning.flink.demo.Utils
import org.apache.flink.streaming.api.scala._

/**
 *
 *
 * @author ydl
 * @since 2020/9/22
 */
object GroupByDemo extends Utils {

  class WC(val word: String, val count: Int) {
    def this() {
      this(null, -1)
    }

    override def toString: String = word + ":" + count
  }

  def main(args: Array[String]): Unit = {
    readFile("/Volumes/work/code/study/learning-flink/pom.xml").map(_.replace("</", " ")
      .replace("<", " ")
      .replace("/>", " ")
      .replace(">", " ")
      .replace("\"", " ").trim)
      .flatMap(_.split(" "))
      .map(new WC(_, 1))
      //根据类属性分组
      .groupBy(_.word)
      .reduce((w1, w2) => new WC(w1.word, w1.count + w2.count))
      .print()

  }

}
